import java.io.PrintWriter;
import processing.core.*;

public class parseText {
	
	String[] UFO_DATE;
	String[] UFO_TIME;
	String[] UFO_CITY;
	String[] UFO_STATE;
	String[] UFO_SHAPE;
	String[] UFO_DESCRIP;
	
	int i = -1;	//current index to store data.	
	
	public void parseFile() {
		PrintWriter output;

		output = GLOBAL.processing.createWriter("All_Sightings.txt");
		UFO_DATE = new String[100000];
		UFO_TIME = new String[100000];
		UFO_CITY = new String[100000];
		UFO_STATE = new String[100000];
		UFO_SHAPE = new String[100000];
		UFO_DESCRIP = new String[100000];
		
		String dTemp = "";
		
		for (int xXx = 1; xXx<50; ++xXx) {
			String name = "aliens/" + xXx + ".txt";
//			i = -1;
			String[] allLines = GLOBAL.processing.loadStrings(name);
			
			for(int x=0; x<allLines.length; ++x) {
	
				//removes any whitespace character 
				//if it is the first or last character in a line
				allLines[x] = PApplet.trim(allLines[x]);	
			
				String[] oneLine = PApplet.split(allLines[x], "\t");
				
				for(int token_count = 1; token_count < oneLine.length-1; ++token_count){
					oneLine[token_count] = PApplet.trim(oneLine[token_count]);
					
				}
						
				System.out.println(name + " | line: " + (x+1));
			
				if(isStart(oneLine)) {
					//System.out.println("\t^ Start Line");
					dTemp = "";		
					++i;
					
					int temp = oneLine[0].indexOf(" ");
					int temp2 = oneLine[0].indexOf(" ", temp+1);
					//System.out.println("\t\t" + temp +" / " + temp2);
					
					String UFO_DATE_temp = "";
					
					UFO_DATE_temp = oneLine[0].substring(0, temp);
					
					if(UFO_DATE_temp.charAt(1) == '/'){
						String UFO_DATE_temp2 = "0";  //converts month two 2 digit always
						UFO_DATE_temp2 += UFO_DATE_temp;
						UFO_DATE_temp = UFO_DATE_temp2; 
					}
					
					UFO_DATE[i] = UFO_DATE_temp;
//					UFO_DATE[i] = oneLine[0].substring(0, temp);
					UFO_TIME[i] = oneLine[0].substring(temp+1, temp2);
					UFO_CITY[i] = oneLine[1];				
					
					UFO_STATE[i] = oneLine[2];
					
					if(oneLine.length > 3){
						UFO_SHAPE[i] = oneLine[3];
						if(oneLine.length > 4) {
							dTemp += oneLine[4] + " ";
							
							if(oneLine.length > 5) 
								dTemp += oneLine[5];
						}						
					}					
				}				
			
				else if(isEnd(oneLine)) {
					//System.out.println("\t^ End Line");
				
					//Parses all tokens except for last one.
					for(int y = 0; y<oneLine.length-1; ++y) {
						if(oneLine[y].length() > 0)
						dTemp += oneLine[y] + " ";
					}
					UFO_DESCRIP[i] = dTemp;
				}
			
				//Middle Line
				else {
					//if there are only two tokens on the line
					if(oneLine.length == 2){
						//Parse and store entire line
						for(int y=0; y<oneLine.length; ++y) {
							dTemp += oneLine[y] + " ";					
						}
					}
					//if the shape token gets bumped to the second line
					if(oneLine.length == 3){
						UFO_SHAPE[i] = oneLine[0];
						for(int y=1; y<oneLine.length; ++y) {
							dTemp += oneLine[y] + " ";					
						}
					}				
				}
			} //end of parsing a single fil			
		}
		//writes to file	
		for(int j = 0; j < UFO_CITY.length; ++j){
			output.println(UFO_CITY[j] + "\t" + UFO_STATE[j] + "\t" + UFO_SHAPE[j] + "\t" + UFO_DATE[j] + "\t" + UFO_TIME[j] + "\t" + UFO_DESCRIP[j]);
		}
		output.flush();
		output.close();
	} //end parseFile()
		
	
	private boolean isStart(String[] line) {
		return (line.length > 1) && ((line[0].charAt(1) == '/') || (line[0].charAt(2) == '/'));
	}
	
	private boolean isEnd(String[] line) {
		return (line[line.length-1].charAt(1) == '/') || (line[line.length-1].charAt(2) == '/');
	}
		
	
	public void go(){
//		for(int x = 22; x<50; ++x) {
//			parseFile("aliens/" + x + ".txt");
//		}
		parseFile();
	}
}
